<template>
  <div :class="{ active: active, chatItem: true, isOnLines: !isOnLine }">
    <el-badge :value="unreadCount" :hidden="unreadCount === 0">
      <template v-if="img">
        <Avatar :img="img" :name="username" />
      </template>
      <template v-else>
        <Avatar :name="username" />
      </template>
    </el-badge>

    <div class="text">
      <div>
        <span v-if="isGroup">[群]</span><b v-if="nickName">{{ nickName }}</b> -
        {{ username }}
      </div>
      <div class="textContent" v-if="text || atText">
        <div class="atText" v-if="atText">{{ atText }}</div>
        <div class="showText" v-if="text">
          {{ text }}
        </div>
      </div>
    </div>
    <div class="close" v-if="showDel">
      <el-popconfirm title="确定要删除吗?" @confirm="confirmEvent">
        <template #reference>
          <i class="iconfont icon-v-guanbi1"></i>
        </template>
      </el-popconfirm>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, toRefs } from "vue";
import Avatar from "@/components/Avatar.vue";

const props = defineProps({
  id: {
    type: String,
    default: () => "",
  },
  unreadCount: {
    type: Number,
    required: false,
    default: 0,
  },
  img: {
    type: String,
    default: () => "https://i.gtimg.cn/club/item/face/img/2/15922_100.gif",
  },
  username: {
    type: String,
    required: true,
    default: () => "",
  },
  nickName: {
    type: String,
    default: () => "",
  },
  text: {
    type: String,
    default: () => "",
  },
  showDel: {
    type: Boolean,
    default: () => false,
  },
  isGroup: {
    type: Boolean,
    default: () => false,
  },
  active: {
    type: Boolean,
    required: false,
    default: () => false,
  },
  isOnLine: {
    type: Boolean,
    default: () => false,
  },
});
const { nickName, img, atMe, atAll, id, showDel, active, isOnLine } =
  toRefs(props);
import { useChatStore } from "@/store/chatStore";
import { storeToRefs } from "pinia";
const chatStore = useChatStore();
const emit = defineEmits(["del"]);

const confirmEvent = () => {
  console.log("confirmEvent", props.id);
  emit("del", props.id);
};
const { chat } = storeToRefs(chatStore);
const atText = computed(() => {
  const currentChat =
    chatStore.chats.length > 0 &&
    chatStore.chats.find((item) => item.id === id.value);
  if (currentChat && currentChat?.atMe) {
    return "[有人@我]";
  } else if (currentChat && currentChat?.atAll) {
    return "[@全体成员]";
  }
  return "";
});
</script>

<style scoped lang="scss">
.chatItem {
  height: 5rem;
  display: flex;
  margin-bottom: 10px;
  position: relative;
  padding-left: 5px;
  align-items: center;
  .close {
    position: absolute;
    width: 1.5rem;
    height: 1.5rem;
    right: 10px;
    top: 1.825rem;
    display: none;
  }
  &:hover {
    .close {
      display: block;
    }
  }
  .avatar {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 4.4rem;
    height: 4.4rem;
  }
  .text {
    margin-left: 8px;
    // flex: 3;
    // display: flex;
    // flex-direction: column;
    // height: 100%;
    flex-shrink: 0;
    overflow: hidden;
    font-size: 14px;
    // & > div {
    //   display: flex;
    //   justify-content: flex-start;
    //   align-items: center;
    //   flex: 1;

    // }
  }
  .textContent {
    display: flex;
    overflow: hidden;
    margin-top: 5px;
    .atText {
      color: #f0f;
      font-size: 12px;
      margin-right: 5px;
      width: 6rem;
    }
    .showText {
      display: block;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      // color: #fff;
      font-size: 12px;
      width: 10rem;
    }
  }
}

// $color-message-bg  $color-gray
</style>
